<?php
/**
 * CreateTime: 2022/8/5 10:45
 */

namespace App\Service\Approval\ApprovalCreditClient;

use App\Consts\ClientConsts\ClientConsts;
use App\Models\Approval\OldDingTalkApprovalModel;
use App\Models\Client\ClientModel;
use App\Models\Client\CreditClientModel;
use App\Service\Approval\DingTalkApprovalInterFace;
use App\Tools\Log\Log;
use Illuminate\Support\Facades\DB;

class ApprovalCreditClientRemoveBlack implements DingTalkApprovalInterFace
{
    public function approvalStart($processInstanceId, $sourceId, $processStatus)
    {
        # 信用客户审批开始
        $creditClientModel = new CreditClientModel();
        DB::beginTransaction();
        try {
            Log::e("进入开始");
            # 查看移除名单送审字典值
            $auditStatus = app()->gcHelper->getDictionaryInfoByDictionaryKey(ClientConsts::REMOVE_CREDIT_CLIENT_BLACK_AUDIT_WAIT,['dictionary_value' ])['dictionary_value'];
            // 获取信用客户id
            $data = [
                'audit_status' => $auditStatus
            ];
            # 修改信用客户送审表状态为 移除黑名单送审中
            $creditClientModel->editCreditClient($sourceId,$data);
            DB::commit();
            Log::e("正常结束开始");
        }catch (\Exception $exception){
            Log::e($exception);
            DB::rollBack();
        }
    }

    public function approvalAgreeEnd($processInstanceId, $sourceId, $processStatus)
    {
        # 信用客户审批开始
        $creditClientModel = new CreditClientModel();
        DB::beginTransaction();
        try {
            Log::e("进入开始");
            $data = [
                'audit_status' => 1
            ];
            # 修改信用客户送审表状态为 移除黑名单送审中
            $creditClientModel->editCreditClient($sourceId,$data);
            // 获取客户id
            $clientId = $creditClientModel->getCreditClientInfoByCreditClientId($sourceId,[
                'client_id'
            ])['client_id'];
            # 修改客户表状态为普通客户 client_type =1 is_credit_client= 1
            $clientModel = new ClientModel();
            $clientModel->editClient([
                'client_id' => $clientId,
                'client_type' => 1,
                'is_credit_client' => 1
            ]);
            DB::commit();
            Log::e("正常结束开始");
        }catch (\Exception $exception){
            Log::e($exception);
            DB::rollBack();
        }
    }

    public function approvalRefuseEnd($processInstanceId, $sourceId, $processStatus)
    {
        # 信用客户审批开始
        $creditClientModel = new CreditClientModel();
        DB::beginTransaction();
        try {
            Log::e("进入开始");
            # 查看移除名单送审字典值
            $data = [
                'audit_status' => 1
            ];
            # 修改信用客户送审表状态为 移除黑名单送审中
            $creditClientModel->editCreditClient($sourceId,$data);
            DB::commit();
            Log::e("正常结束开始");
        }catch (\Exception $exception){
            Log::e($exception);
            DB::rollBack();
        }
    }
}